package com.yu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.controller.Code;
import com.yu.controller.dto.SelectModel;
import com.yu.controller.dto.StuInfoDto;
import com.yu.entity.StuInfo;
import com.yu.entity.User;
import com.yu.exception.BusinessException;
import com.yu.mapper.StuInfoMapper;
import com.yu.service.StuInfoService;
import com.yu.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class StuInfoServiceImpl implements StuInfoService {
    @Autowired
    private StuInfoMapper stuInfoMapper;
    @Override
    public Map<String, Object> selectByPageAndCondition(int currentPage, int pageSize, SelectModel selectModel) {
        IPage<StuInfo> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<StuInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(null != selectModel.getAdmissionyear(), StuInfo::getAdmissionyear, selectModel.getAdmissionyear());
        lqw.eq(null != selectModel.getAcademy(), StuInfo::getAcademy, selectModel.getAcademy());

        lqw.like(null != selectModel.getNumber() && !(StringUtils.isEmpty(selectModel.getNumber())), StuInfo::getNumber, selectModel.getNumber());
        lqw.like(null != selectModel.getName() && !(StringUtils.isEmpty(selectModel.getName())), StuInfo::getName, selectModel.getName());
        lqw.like(null != selectModel.getMajor() && !(StringUtils.isEmpty(selectModel.getMajor())), StuInfo::getMajor, selectModel.getMajor());

        IPage<StuInfo> stuInfoIPage = stuInfoMapper.selectPage(page, lqw);

        List<StuInfo> records = stuInfoIPage.getRecords();
        long totalCount = stuInfoIPage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("totalCount", totalCount);
        map.put("stuInfos", records);
        return map;
    }

    @Override
    public Boolean save(StuInfo stuInfo) {
        int flag = stuInfoMapper.insert(stuInfo);
        return flag > 0;
    }

    @Override
    public StuInfo selectById(int id) {
        StuInfo stuInfo = stuInfoMapper.selectById(id);
        return stuInfo;
    }

    @Override
    public Boolean update(StuInfo stuInfo) {
        int num = stuInfoMapper.updateById(stuInfo);
        return num > 0;
    }

    @Override
    public Boolean deleteOneById(Integer id) {
        int num = stuInfoMapper.deleteById(id);
        return num > 0;
    }

    @Override
    public Boolean deleteByIds(List<Integer> ids) {
        int num = stuInfoMapper.deleteBatchIds(ids);
        return num > 0;
    }

    @Override
    public Boolean insertAll(List<StuInfo> stuInfos) {
        Boolean flag =true;
        for (int i = 0; i < stuInfos.size(); i++) {
            int insert = stuInfoMapper.insert(stuInfos.get(i));
            flag = insert > 0 ? true : false;
        }
        return flag;
    }

    // 导出excel文件接口，需查询id字段以外的其余字段信息
    @Override
    public List<StuInfo> selectAllExecptId() {
        QueryWrapper<StuInfo> qw= new QueryWrapper();
        qw.select("number","name","sex","age","phone","password",
                "major","aclass","academy","admissiondate","policalstatus",
                "nation","graducatedschool","address","gkscore",
                "wechat","qq","birthdate",
                "admissionyear","province","status");
        List<StuInfo> stuInfos = stuInfoMapper.selectList(qw);
        return stuInfos;
    }

    @Override
    public StuInfo selectOne(User user) {

        LambdaQueryWrapper<StuInfo> lqw = new LambdaQueryWrapper();
        lqw.eq(StuInfo::getNumber,user.getUsername());
        lqw.eq(StuInfo::getPassword,user.getPassword());
        StuInfo one = stuInfoMapper.selectOne(lqw);

        return one;
    }

    @Override
    public Boolean handleStuSign(Integer id) {
        // 获取当前签到时间
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String strDate = StrUtil.toString(dateFormat.format(calendar.getTime()));

        Integer i = stuInfoMapper.handleStuSignIn(id,strDate);
        return i > 0;
    }

}